using System;
using Core.Event;
using FT.Tweening;
using Game;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;

public class HUD : MonoBehaviour {
    public Text TxtCoin;
    public int CoinUpdateFrame;
    
    private void Start() {
        GameEventMgr.Ins.RegisterEvent(GameEventID.PlayerInit,onPlayerInit);
        GameEventMgr.Ins.RegisterEvent(GameEventID.CoinValueChanged,onCoinValueChanged);
    }

    private void OnDestroy() {
        GameEventMgr.Ins.UnRegisterEvent(GameEventID.PlayerInit,onPlayerInit);
        GameEventMgr.Ins.UnRegisterEvent(GameEventID.CoinValueChanged,onCoinValueChanged);
    }

    private void onPlayerInit(GameEvent evt) {
        TxtCoin.text = PlayerManager.Ins.CurPlayer.TradeModule.Money.ToString();
    }
    private void onCoinValueChanged(GameEvent evt) {
        var lastValue = int.Parse(TxtCoin.text);
        var curValue = PlayerManager.Ins.CurPlayer.TradeModule.Money;
        FrameTween.To(() => lastValue, (value) => lastValue = value, curValue, CoinUpdateFrame).SetEase(Ease.OutCirc)
            .OnUpdate(() => {
                TxtCoin.text = lastValue.ToString();
            });
    }

}
